package com.rxyl.love.petdoctor.acticity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.adapter.DistrictAdapter;
import com.rxyl.love.petdoctor.entity.DistrictEntity;
import com.rxyl.love.petdoctor.fragment.HospitalFragment;
import com.rxyl.love.petdoctor.util.CityJSON;
import com.rxyl.love.petdoctor.util.NetworkUtil;
import com.rxyl.love.petdoctor.util.SharedUtil;

import java.util.List;

public class DistrictActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView lvDistrict;
    private DistrictAdapter adapter;
    private int cityid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_district);
        init();
        loadData();
    }

    private void init() {
        lvDistrict = (ListView) findViewById(R.id.lv_district);
        lvDistrict.setOnItemClickListener(this);
    }

    private void loadData() {
        cityid = SharedUtil.getInt("cityid");
        NetworkUtil.getInstance().getCityList(new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                List<DistrictEntity> list = CityJSON.getDistrictByJSON(responseInfo.result.toString(),
                        cityid);
                if (list != null) {
                    adapter = new DistrictAdapter(getApplicationContext(), list);
                    lvDistrict.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        DistrictEntity entity = (DistrictEntity) adapter.getItem(position);
        Intent intent = getIntent();
        intent.putExtra("district", entity);
        setResult(HospitalFragment.RESULT_DISTRICT, intent);
        finish();
    }

    public void backTo(View view) {
        this.finish();
    }
}
